﻿using Liuliu.ScriptEngine;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GmPrizedrawTool
{
    /// <summary>
    /// 移动玩家
    /// </summary>
    internal class MoveUser
    {
        /// <summary>
        /// 开始批量移动玩家
        /// </summary>
        /// <param name="Hwnd">程序句柄</param>
        /// <param name="moveUserNum">移动玩家数量</param>
        /// <param name="movePoint">移动坐标</param>
        public static void StartMoveUser(int Hwnd, int moveUserNum, string movePoint)
        {
            DmPlugin dm = Program.dm;
            List<UserInfoModel> userList = GetRandomOnlineUserList(moveUserNum);
            string NpcInfo = GetRandomNpc();
            string NpcId = NpcInfo.Split('_')[0];
            string NpcName = NpcInfo.Split('_')[1];
            if (userList != null && userList.Count > 0)
            {
                //发送指令，移动到指定位置
                dm.SendString(Hwnd, $"-moveto {movePoint.Replace(',', ' ')}");
                dm.KeyPress(13);

                //直接创建NPC
                dm.SendString(Hwnd, $"-createnpc {NpcId} {NpcName}");
                dm.KeyPress(13);

                foreach (UserInfoModel user in userList)
                {
                    //发送指令，移动玩家到指定位置
                    dm.SendString(Hwnd, $"-moveuserto {user.UserName} {movePoint.Replace(',', ' ')}");
                    dm.KeyPress(13);//回车发送指令
                    Thread.Sleep(10);
                }
            }
            else {
                Console.WriteLine("未获取到在线玩家信息");
                return;
            }
        }

        /// <summary>
        /// 随机获取一个NPC信息
        /// </summary>
        public static string GetRandomNpc()
        {
            //string NpcInfo = Program.NpcInfo;
            //string[] Npc = NpcInfo.ToString().Split(',');
            //Random Random = new Random();
            //int RandomVal = Random.Next(0, Npc.Length);
            //return Npc[RandomVal];
            return "20004_沙石老怪";
        }

        /// <summary>
        /// 获取指定数量的在线玩家
        /// </summary>
        public static List<UserInfoModel> GetRandomOnlineUserList(int userCount)
        {
            try
            {
                string serverAddress = $"{Program.ApiAddress}Api/Interface/GetGamesUserInfo?ServerId={Program.serverModel.Id}&Num={userCount}";
                string serverInfoStr = CommonFun.HttpGet(serverAddress);
                if (serverInfoStr.Contains("获取失败"))
                {
                    return null;
                }
                else
                {
                    List<UserInfoModel> UserList = JsonConvert.DeserializeObject<List<UserInfoModel>>(serverInfoStr);
                    if (UserList != null && UserList.Count > 0)
                    {
                        return UserList;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"远程获取待移动玩家列表出现错误，{e.Message}");
            }
            return null;
        }

        /// <summary>
        ///发送公告信息
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static bool SendOneNotice(string Content)
        {
            string NoticeApiUrl = $"{Program.GmToolDomain}SendNotice";
            string FormData = $"content={Content}";
            string ReturnVal = CommonFun.HttpPostRaw(NoticeApiUrl, Content);
            if (ReturnVal == "200")
            {
                return true;
            }
            return false;
        }

    }
}
